var express = require('express');
var router = express.Router();
require("./mongoose")

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});





//增加
router.get('/receiplanlistadd', function (req, res, next) {
    var a = req.query;
    // console.log(a)
    var addstockmodel = new stockmodel();
    addstockmodel.num = a.num;
    addstockmodel.supplier = a.supplier;
    addstockmodel.foodname = a.foodname;
    addstockmodel.quantity = a.quantity;
    addstockmodel.quantity2 = a.quantity2;
    addstockmodel.quantity3 = a.quantity3;
    addstockmodel.quantity4 = a.quantity4;
    addstockmodel.company = a.company;
    addstockmodel.budget = a.budget;
    addstockmodel.budmoney = a.budmoney;
    addstockmodel.remarks = a.remarks;
    addstockmodel.newtime = a.newtime;
    addstockmodel.number = a.number;
    addstockmodel.edid = a.edid;
    addstockmodel.money = a.money;
    addstockmodel.rknumber = a.rknumber;
    addstockmodel.state ="未签收";
    addstockmodel.signtime = "---";
    addstockmodel.signed ="---";
    addstockmodel.signrem = "---";
    addstockmodel.verifier="---";
    addstockmodel.custodian="---";
    addstockmodel.staterk="未入库";
    addstockmodel.remarksrk="---";
    addstockmodel.delnumber = a.delnumber;
    addstockmodel.isflag = "未审核";
    addstockmodel.save(function (err) {
      // console.log(data)
        if (!err) {
            console.log("数据保存成功");
            res.send('1')
        } else {
            console.log("数据保存失败");
            res.send('0')
        }
    })
});

//删除数据
router.get('/delestock', function(req, res) {
  var ids = req.query.data;
//  console.log(ids)
  //遍历删除数据
  for (let i = 0; i < ids.length; i++) {
  	stockmodel.findById(ids[i]).exec(function (err,data) {
	  	data.remove(function (err) {	
			  if(err){
				  res.send("0")
			  }	
	  	})
	  })
  }
 res.send("1")
});

//审核
router.get('/statemore', function(req, res) {
	var idall = req.query.data;
	for(let i=0;i<idall.length;i++){
		stockmodel.findById(idall[i]).exec(function(err,data){
			//var insertuser = new stockmodel();//new一下创建的数据库
			data.isflag = "已审核";//序号
			data.save(function(err){
				if(err){
					console.log(err)
					res.send("0")
				}
			})
		})
	}
	res.send("1")	
})

//发送页面传递的id对应的data
router.get('/receiptplandit', function(req, res) {
  var id = req.query.str;
  stockmodel.findById(id).exec(function (err,data) {
  	res.send(data)
  })
  
});
//将页面的值对应id进行修改
router.post("/receiplanlistdit",function (req,res) {
	var obj = req.body;
	/* num,supplier,foodname,quantity,company, budget,budmoney,remarks,number,newtime,money,edid*/             
	stockmodel.findById(obj.edid).exec(function (err,data) {
	  	data.num = obj.num;
	  	data.supplier = obj.supplier;
	  	data.foodname = obj.foodname;
	  	data.quantity = obj.quantity;
	  	data.company = obj.company;
	  	data.budget = obj.budget;
	  	data.remarks = obj.remarks;
	  	data.budmoney = obj.budmoney;
	  	data.number = obj.number;
	  	data.newtime = obj.newtime;
	  	data.money = obj.money;
	  	data.save(function (err) {
	  		if (err) {
	  			res.send("0")
	  		}else{
	  			res.send("1")
	  		}
	  	})
	})
})
//获取总条数
router.get('/count', function(req, res) {
	stockmodel.find({}).exec(function (err,data) {
  	var count = data.length;
  	res.send({count})
  })
})

//页面加载时调用数据和传值
router.get('/stocklist', function(req, res) {
  var datas = req.query;
  // console.log(datas)
  /*
   根据前台发送过来的数据，进行分页
   1. 默认每一页的条数
   2. 查询数据
   	skip()  limit()
   * */
  var page = datas.n;//当前页码
  var pagenum = 3;//每一页显示的条数
  var skipnum = (page-1)*pagenum;
  //跳过skipnum条,显示pagenum条
  stockmodel.find({}).skip(skipnum).limit(pagenum).exec(function (err,data) {
  	res.send(data)
  })
});
// //进货签收表单读取
// router.get('/delform',function(req,res){
//   stockmodel.find({isflag :"已审核"}).exec(function(err,data){
// 		// console.log(data)
// 		res.send(data)
// 	})
// })

//进货签收表单增加
router.post("/addsign",function (req,res) {
	var obj = req.body;
  // console.log(obj)
  var insertuser = new stockmodel();
    insertuser.num=obj.num;
    insertuser.supplier=obj.supplier;
    insertuser.supplierid=obj.supplierid;
    insertuser.supname=obj.supname;
    insertuser.tele=obj.tele;
    insertuser.foodname=obj.foodname;
    insertuser.endname=obj.endname;
    insertuser.endbank=obj.endbank;
    insertuser.endnumber=obj.endnumber;
    insertuser.number=obj.number;
    insertuser.delnumber=obj.delnumber;
    insertuser.newtime=obj.newtime;
    insertuser.state ="未签收";
    insertuser.isflag = "已审核";
    insertuser.save(function (err) {
    if(err){
      res.send("0");
    }else{
      res.send("1")
    }
  })
})

//进货.签收.入库.表单删除
router.get('/deletemore', function(req, res) {
  var ids = req.query.data;
//  console.log(ids)
  //遍历删除数据
  for (let i = 0; i < ids.length; i++) {
  	stockmodel.findById(ids[i]).exec(function (err,data) {
	  	data.remove(function (err) {	
			  if(err){
				  res.send("0")
			  }	
	  	})
	  })
  }
 res.send("1")
});

//修改进货签收表单
//1---将id对应的值传入前端网页
router.get('/supplierdit', function(req, res) {
  var id = req.query.str;
  stockmodel.findById(id).exec(function (err,data) {
  	res.send(data)
  })
});
//2---查询数据,渲染首页
router.post("/editsign",function (req,res) {
	var obj = req.body;
	stockmodel.findById(obj.edid).exec(function (err,data) {
    data.num=obj.num;
    data.supplier=obj.supplier;
    data.supplierid=obj.supplierid;
    data.supname=obj.supname;
    data.tele=obj.tele;
    data.foodname=obj.foodname;
    data.endname=obj.endname;
    data.endbank=obj.endbank;
    data.endnumber=obj.endnumber;
    data.number=obj.number;
    data.delnumber=obj.delnumber;
    data.newtime=obj.newtime;
    data.state ="未签收";
    data.isflag = "已审核";
    data.save(function (err) {
      if (err) {
        res.send("0")
      }else{
        res.send("1")
      }
    })
	})
})

//签收
router.post("/editsignlist",function (req,res) {
	var obj = req.body;
	stockmodel.findById(obj.ide).exec(function (err,data) {
    data.signrem=obj.signrem;
    data.signed=obj.signed;
    data.supname=obj.supname;
    data.signtime=obj.signtime;
    data.state ="已签收";
    data.save(function (err) {
      if (err) {
        res.send("0")
      }else{
        res.send("1")
      }
    })
	})
})
//详情页
router.get('/sgdetail', function(req, res, next) {
	var f = req.query.str;
	stockmodel.findById(f).exec(function(err,data){
		res.send(data)
    // console.log(data)
	})
});

//查询搜索功能
router.get('/delform', function(req, res) {
  var datas = req.query;
  var obj = {isflag:"已审核"};
  if (datas.s_supplier != "全部") {
  	obj.supplier = datas.s_supplier;
  }
  if (datas.s_state != "全部") {
  	obj.state = datas.s_state;
  }
  stockmodel.find(obj).exec(function (err,data) {
  	res.send(data)
    // console.log(data)
  })
});

//入库渲染
router.get('/boundlist',function(req,res){
   stockmodel.find({state:"已签收"}).exec(function (err,data) {
  	res.send(data)
    // console.log(data)
   })
})
//入库修改
router.post("/boundindetail",function (req,res) {
	var obj = req.body;
	/*  num,supplier,foodname, quantity,company,budget, budmoney,remarks, signed,verifier,custodian,remarksrk,signtime,staterk,edid*/             
	stockmodel.findById(obj.edid).exec(function (err,data) {
	  	data.num = obj.num;
	  	data.supplier = obj.supplier;
	  	data.foodname = obj.foodname;
	  	data.quantity = obj.quantity;
	  	data.company = obj.company;
	  	data.budget = obj.budget;
	  	data.remarks = obj.remarks;
	  	data.budmoney = obj.budmoney;
	  	data.signed = obj.signed;
	  	data.verifier = obj.verifier;
	  	data.custodian = obj.custodian;
	  	data.remarksrk = obj.remarksrk;
	  	data.signtime = obj.signtime;
	  	data.staterk = obj.staterk;
	  	data.save(function (err) {
	  		if (err) {
	  			res.send("0")
	  		}else{
	  			res.send("1")
	  		}
	  	})
	})
})


//出库渲染
router.get('/boundlist2',function(req,res){
   stockmodel.find({staterk:"已入库"}).exec(function (err,data) {
  	res.send(data)
    // console.log(data)
   })
})

module.exports = router;